package com.example.library.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * 分页响应结果类
 *
 * @param <T> 数据类型
   */
@Data
@EqualsAndHashCode(callSuper = true)
public class PageResult<T> extends Result<List<T>> {

    /**
     * 当前页码
     */
    private Long current;

    /**
     * 每页大小
     */
    private Long size;

    /**
     * 总记录数
     */
    private Long total;

    /**
     * 总页数
     */
    private Long pages;

    public PageResult() {
        super();
    }

    public PageResult(IPage<T> page) {
        super(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), page.getRecords());
        this.current = page.getCurrent();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.pages = page.getPages();
    }

    public PageResult(Long current, Long size, Long total, List<T> records) {
        super(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), records);
        this.current = current;
        this.size = size;
        this.total = total;
        this.pages = (total + size - 1) / size;
    }

    /**
     * 成功分页响应
     */
    public static <T> PageResult<T> success(IPage<T> page) {
        return new PageResult<>(page);
    }

    /**
     * 成功分页响应（自定义数据）
     */
    public static <T> PageResult<T> success(Long current, Long size, Long total, List<T> records) {
        return new PageResult<>(current, size, total, records);
    }

    /**
     * 空分页响应
     */
    public static <T> PageResult<T> empty(Long current, Long size) {
        return new PageResult<>(current, size, 0L, List.of());
    }

}